<div class="body" role="main"><div class="section" id="module-tarfile"><h1><span class="yiyi-st" id="yiyi-10">13.6. <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a> — 读取与压缩tar文件</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码：</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/tarfile.py">Lib/tarfile.py</a></span></p><p><span class="yiyi-st" id="yiyi-12"><a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a> 模块用于读取或者压缩tar文件，可针对gzip、bz2或lzma等格式文件进行相关操作。</span><span class="yiyi-st" id="yiyi-13">假如希望对<code class="file docutils literal"><span class="pre">.zip</span></code>格式文件进行操作，则应使用<a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">zipfile</span></code></a>模块，或者使用<a class="reference internal" href="shutil.html#archiving-operations"><span>shutil</span></a>里的高级函数。</span></p><p><span class="yiyi-st" id="yiyi-14">一些事实和数字：</span></p><ul class="simple"><li><span class="yiyi-st" id="yiyi-15">如果相应的模块可用，则读取并写入<a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a>，<a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal"><span class="pre">bz2</span></code></a>和<a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal"><span class="pre">lzma</span></code></a>压缩归档。</span></li><li><span class="yiyi-st" id="yiyi-16">读/写支持POSIX.1-1988（ustar）格式。</span></li><li><span class="yiyi-st" id="yiyi-17">对包括<em>longname</em>和<em>longlink</em>扩展的GNU tar格式的读/写支持，对<em>稀疏</em>扩展的所有变体的只读支持，包括恢复稀疏文件。</span></li><li><span class="yiyi-st" id="yiyi-18">对POSIX.1-2001（pax）格式的读/写支持。</span></li><li><span class="yiyi-st" id="yiyi-19">处理目录，常规文件，硬链接，符号链接，fifos，字符设备和块设备，并能够获取和恢复文件信息，如时间戳，访问权限和所有者。</span></li></ul><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-20"><span class="versionmodified">在版本3.3中更改：</span>添加对<a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal"><span class="pre">lzma</span></code></a>压缩的支持。</span></p></div><dl class="function"><dt id="tarfile.open"><span class="yiyi-st" id="yiyi-21"> <code class="descclassname">tarfile.</code><code class="descname">open</code><span class="sig-paren">(</span><em>name=None</em>, <em>mode='r'</em>, <em>fileobj=None</em>, <em>bufsize=10240</em>, <em>**kwargs</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-22">为路径名<em>名称</em>返回<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-23">有关<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象和允许的关键字参数的详细信息，请参阅<a class="reference internal" href="#tarfile-objects"><span>TarFile Objects</span></a>。</span></p><p><span class="yiyi-st" id="yiyi-24"><em>mode</em>必须是<code class="docutils literal"><span class="pre">'filemode[:compression]'</span></code>形式的字符串，默认为<code class="docutils literal"><span class="pre">'r'</span></code>。</span><span class="yiyi-st" id="yiyi-25">这里是模式组合的完整列表：</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-26">模式</span></th><th class="head"><span class="yiyi-st" id="yiyi-27">行动</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-28"><code class="docutils literal"><span class="pre">'r'</span> <span class="pre">或</span> <span class="pre">'r：*'</span></code></span></td><td><span class="yiyi-st" id="yiyi-29">通过透明压缩打开阅读（推荐）。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-30"><code class="docutils literal"><span class="pre">'r:'</span></code></span></td><td><span class="yiyi-st" id="yiyi-31">无压缩打开专用读取。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-32"><code class="docutils literal"><span class="pre">'r:gz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-33">使用gzip压缩打开阅读。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-34"><code class="docutils literal"><span class="pre">'r:bz2'</span></code></span></td><td><span class="yiyi-st" id="yiyi-35">使用bzip2压缩打开阅读。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-36"><code class="docutils literal"><span class="pre">'r:xz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-37">使用lzma压缩打开阅读。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-38"><code class="docutils literal"><span class="pre">'x'</span></code>或<code class="docutils literal"><span class="pre">'x:'</span></code></span></td><td><span class="yiyi-st" id="yiyi-39">完全无压缩地创建tarfile。</span><span class="yiyi-st" id="yiyi-40">引发<a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></code></a>异常（如果它已存在）。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-41"><code class="docutils literal"><span class="pre">'x:gz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-42">使用gzip压缩创建tarfile。</span><span class="yiyi-st" id="yiyi-43">引发<a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></code></a>异常（如果它已存在）。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-44"><code class="docutils literal"><span class="pre">'x:bz2'</span></code></span></td><td><span class="yiyi-st" id="yiyi-45">使用bzip2压缩创建tarfile。</span><span class="yiyi-st" id="yiyi-46">引发<a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></code></a>异常（如果它已存在）。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-47"><code class="docutils literal"><span class="pre">'x:xz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-48">使用lzma压缩创建tarfile。</span><span class="yiyi-st" id="yiyi-49">引发<a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></code></a>异常（如果它已存在）。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-50"><code class="docutils literal"><span class="pre">'a'</span> <span class="pre">或</span> <span class="pre">'a：'</span></code></span></td><td><span class="yiyi-st" id="yiyi-51">打开，无需压缩。</span><span class="yiyi-st" id="yiyi-52">如果文件不存在，则创建该文件。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-53"><code class="docutils literal"><span class="pre">'w'</span> <span class="pre">或</span> <span class="pre">'w：'</span></code></span></td><td><span class="yiyi-st" id="yiyi-54">打开未压缩的写入。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-55"><code class="docutils literal"><span class="pre">'w:gz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-56">打开gzip压缩写入。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-57"><code class="docutils literal"><span class="pre">'w:bz2'</span></code></span></td><td><span class="yiyi-st" id="yiyi-58">打开bzip2压缩写。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-59"><code class="docutils literal"><span class="pre">'w:xz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-60">打开lzma压缩写。</span></td></tr></tbody></table><p><span class="yiyi-st" id="yiyi-61">请注意，不能使用<code class="docutils literal"><span class="pre">'a:gz'</span></code>，<code class="docutils literal"><span class="pre">'a:bz2'</span></code>或<code class="docutils literal"><span class="pre">'a:xz'</span></code></span><span class="yiyi-st" id="yiyi-62">如果<em>模式</em>不适合打开某个（压缩）文件进行读取，则会出现<a class="reference internal" href="#tarfile.ReadError" title="tarfile.ReadError"><code class="xref py py-exc docutils literal"><span class="pre">ReadError</span></code></a>。</span><span class="yiyi-st" id="yiyi-63">使用<em>模式</em> <code class="docutils literal"><span class="pre">'r'</span></code>可避免此情况。</span><span class="yiyi-st" id="yiyi-64">如果不支持压缩方法，则会引发<a class="reference internal" href="#tarfile.CompressionError" title="tarfile.CompressionError"><code class="xref py py-exc docutils literal"><span class="pre">CompressionError</span></code></a>。</span></p><p><span class="yiyi-st" id="yiyi-65">如果指定<em>fileobj</em>，它将用作<em>名称</em>以二进制模式打开的<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>的替代。</span><span class="yiyi-st" id="yiyi-66">它应该在位置0。</span></p><p><span class="yiyi-st" id="yiyi-67">For modes <code class="docutils literal"><span class="pre">'w:gz'</span></code>, <code class="docutils literal"><span class="pre">'r:gz'</span></code>, <code class="docutils literal"><span class="pre">'w:bz2'</span></code>, <code class="docutils literal"><span class="pre">'r:bz2'</span></code>, <code class="docutils literal"><span class="pre">'x:gz'</span></code>, <code class="docutils literal"><span class="pre">'x:bz2'</span></code>, <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal"><span class="pre">tarfile.open()</span></code></a> accepts the keyword argument <em>compresslevel</em> (default <code class="docutils literal"><span class="pre">9</span></code>) to specify the compression level of the file.</span></p><p><span class="yiyi-st" id="yiyi-68">对于特殊用途，还有<em>模式</em>的第二种格式：<code class="docutils literal"><span class="pre">'filemode|[compression]'</span></code>。</span><span class="yiyi-st" id="yiyi-69"><a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal"><span class="pre">tarfile.open()</span></code></a>将返回一个<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象，该对象将其数据作为块流处理。</span><span class="yiyi-st" id="yiyi-70">不会对文件进行随机搜索。</span><span class="yiyi-st" id="yiyi-71">如果给定，<em>fileobj</em>可以是具有<code class="xref py py-meth docutils literal"><span class="pre">read()</span></code>或<code class="xref py py-meth docutils literal"><span class="pre">write()</span></code>方法的任何对象（取决于<em>t5&gt;）。</em></span><span class="yiyi-st" id="yiyi-72"><em>bufsize</em>指定块大小并默认为<code class="docutils literal"><span class="pre">20</span> <span class="pre">*</span> <span class="pre">512</span></code>字节。</span><span class="yiyi-st" id="yiyi-73">使用此变体与例如</span><span class="yiyi-st" id="yiyi-74"><code class="docutils literal"><span class="pre">sys.stdin</span></code>，套接字<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>或磁带设备。</span><span class="yiyi-st" id="yiyi-75">然而，这样的<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象受限，因为它不允许随机访问，参见<a class="reference internal" href="#tar-examples"><span>Examples</span></a>。</span><span class="yiyi-st" id="yiyi-76">当前可能的模式：</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-77">模式</span></th><th class="head"><span class="yiyi-st" id="yiyi-78">行动</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-79"><code class="docutils literal"><span class="pre">'r|*'</span></code></span></td><td><span class="yiyi-st" id="yiyi-80">打开用于透明压缩读取的tar块的<em>流</em>。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-81"><code class="docutils literal"><span class="pre">'r|'</span></code></span></td><td><span class="yiyi-st" id="yiyi-82">打开未压缩的tar块的<em>流</em>以进行读取。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-83"><code class="docutils literal"><span class="pre">'r|gz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-84">打开gzip压缩的<em>流</em>进行阅读。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-85"><code class="docutils literal"><span class="pre">'r|bz2'</span></code></span></td><td><span class="yiyi-st" id="yiyi-86">打开bzip2压缩的<em>流</em>进行阅读。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-87"><code class="docutils literal"><span class="pre">'r|xz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-88">打开lzma压缩的<em>流</em>进行阅读。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-89"><code class="docutils literal"><span class="pre">'w|'</span></code></span></td><td><span class="yiyi-st" id="yiyi-90">打开未压缩的<em>流</em>进行写入。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-91"><code class="docutils literal"><span class="pre">'w|gz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-92">打开gzip压缩的<em>流</em>进行写入。</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-93"><code class="docutils literal"><span class="pre">'w|bz2'</span></code></span></td><td><span class="yiyi-st" id="yiyi-94">打开用于写入的bzip2压缩<em>流</em>。</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-95"><code class="docutils literal"><span class="pre">'w|xz'</span></code></span></td><td><span class="yiyi-st" id="yiyi-96">打开lzma压缩的<em>流</em>进行写入。</span></td></tr></tbody></table><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-97"><span class="versionmodified">在版本3.5中已更改：</span>添加了<code class="docutils literal"><span class="pre">'x'</span></code>（独占创建）模式。</span></p></div></dd></dl><dl class="class"><dt id="tarfile.TarFile"><span class="yiyi-st" id="yiyi-98"> <em class="property">class </em><code class="descclassname">tarfile.</code><code class="descname">TarFile</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-99">读写tar存档的类。</span><span class="yiyi-st" id="yiyi-100">不要直接使用这个类：use <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal"><span class="pre">tarfile.open()</span></code></a>。</span><span class="yiyi-st" id="yiyi-101">请参阅<a class="reference internal" href="#tarfile-objects"><span>TarFile Objects</span></a>。</span></p></dd></dl><dl class="function"><dt id="tarfile.is_tarfile"><span class="yiyi-st" id="yiyi-102"> <code class="descclassname">tarfile.</code><code class="descname">is_tarfile</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-103">返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>如果<em>名称</em>是tar存档文件，<a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>模块可以读取。</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-104"><a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>模块定义了以下例外：</span></p><dl class="exception"><dt id="tarfile.TarError"><span class="yiyi-st" id="yiyi-105"> <em class="property">exception </em><code class="descclassname">tarfile.</code><code class="descname">TarError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-106">所有<a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>异常的基类。</span></p></dd></dl><dl class="exception"><dt id="tarfile.ReadError"><span class="yiyi-st" id="yiyi-107"> <em class="property">exception </em><code class="descclassname">tarfile.</code><code class="descname">ReadError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-108">在tar文件打开时引发，或者无法通过<a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>模块处理，或者以某种方式无效。</span></p></dd></dl><dl class="exception"><dt id="tarfile.CompressionError"><span class="yiyi-st" id="yiyi-109"> <em class="property">exception </em><code class="descclassname">tarfile.</code><code class="descname">CompressionError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-110">当不支持压缩方法或无法正确解码数据时引发。</span></p></dd></dl><dl class="exception"><dt id="tarfile.StreamError"><span class="yiyi-st" id="yiyi-111"> <em class="property">exception </em><code class="descclassname">tarfile.</code><code class="descname">StreamError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-112">对于流式<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象的典型限制而引发。</span></p></dd></dl><dl class="exception"><dt id="tarfile.ExtractError"><span class="yiyi-st" id="yiyi-113"> <em class="property">exception </em><code class="descclassname">tarfile.</code><code class="descname">ExtractError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-114">使用<a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal"><span class="pre">TarFile.extract()</span></code></a>时，但仅在<code class="xref py py-attr docutils literal"><span class="pre">TarFile.errorlevel</span></code> <code class="docutils literal"><span class="pre">==时产生<em>非致命</em>错误</span> <span class="pre">2</span></code>。</span></p></dd></dl><dl class="exception"><dt id="tarfile.HeaderError"><span class="yiyi-st" id="yiyi-115"> <em class="property">exception </em><code class="descclassname">tarfile.</code><code class="descname">HeaderError</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-116">由<a class="reference internal" href="#tarfile.TarInfo.frombuf" title="tarfile.TarInfo.frombuf"><code class="xref py py-meth docutils literal"><span class="pre">TarInfo.frombuf()</span></code></a>引发，如果它获得的缓冲区无效。</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-117">以下常量在模块级可用：</span></p><dl class="data"><dt id="tarfile.ENCODING"><span class="yiyi-st" id="yiyi-118"> <code class="descclassname">tarfile.</code><code class="descname">ENCODING</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-119">Windows上的默认字符编码：<code class="docutils literal"><span class="pre">'utf-8'</span></code>否则为<a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal"><span class="pre">sys.getfilesystemencoding()</span></code></a>返回的值。</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-120">以下每个常数定义<a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>模块能够创建的tar归档格式。</span><span class="yiyi-st" id="yiyi-121">有关详细信息，请参阅<a class="reference internal" href="#tar-formats"><span>Supported tar formats</span></a>一节。</span></p><dl class="data"><dt id="tarfile.USTAR_FORMAT"><span class="yiyi-st" id="yiyi-122"> <code class="descclassname">tarfile.</code><code class="descname">USTAR_FORMAT</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-123">POSIX.1-1988（ustar）格式。</span></p></dd></dl><dl class="data"><dt id="tarfile.GNU_FORMAT"><span class="yiyi-st" id="yiyi-124"> <code class="descclassname">tarfile.</code><code class="descname">GNU_FORMAT</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-125">GNU tar格式。</span></p></dd></dl><dl class="data"><dt id="tarfile.PAX_FORMAT"><span class="yiyi-st" id="yiyi-126"> <code class="descclassname">tarfile.</code><code class="descname">PAX_FORMAT</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-127">POSIX.1-2001（pax）格式。</span></p></dd></dl><dl class="data"><dt id="tarfile.DEFAULT_FORMAT"><span class="yiyi-st" id="yiyi-128"> <code class="descclassname">tarfile.</code><code class="descname">DEFAULT_FORMAT</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-129">用于创建归档的默认格式。</span><span class="yiyi-st" id="yiyi-130">目前为<a class="reference internal" href="#tarfile.GNU_FORMAT" title="tarfile.GNU_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">GNU_FORMAT</span></code></a>。</span></p></dd></dl><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-131">也可以看看</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-132">模块<a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">zipfile</span></code></a></span></dt><dd><span class="yiyi-st" id="yiyi-133"><a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">zipfile</span></code></a>标准模块的文档。</span></dd><dt><span class="yiyi-st" id="yiyi-134"><a class="reference internal" href="shutil.html#archiving-operations"><span>Archiving operations</span></a></span></dt><dd><span class="yiyi-st" id="yiyi-135">由标准<a class="reference internal" href="shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal"><span class="pre">shutil</span></code></a>模块提供的高级存档工具的文档。</span></dd><dt><span class="yiyi-st" id="yiyi-136"><a class="reference external" href="https://www.gnu.org/software/tar/manual/html_node/Standard.html">GNU tar手动，基本tar格式</a></span></dt><dd><span class="yiyi-st" id="yiyi-137">tar归档文件的文档，包括GNU tar扩展。</span></dd></dl></div><div class="section" id="tarfile-objects"><h2><span class="yiyi-st" id="yiyi-138">13.6.1. </span><span class="yiyi-st" id="yiyi-139">TarFile Objects</span></h2><p><span class="yiyi-st" id="yiyi-140"><a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象提供了一个tar归档文件的接口。</span><span class="yiyi-st" id="yiyi-141">tar归档是一系列块。</span><span class="yiyi-st" id="yiyi-142">存档成员（存储文件）由标题块后跟数据块组成。</span><span class="yiyi-st" id="yiyi-143">可以将文件存储在tar存档中多次。</span><span class="yiyi-st" id="yiyi-144">每个归档成员由<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象表示，有关详细信息，请参阅<a class="reference internal" href="#tarinfo-objects"><span>TarInfo Objects</span></a>。</span></p><p><span class="yiyi-st" id="yiyi-145"><a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象可以在<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a>语句中用作上下文管理器。</span><span class="yiyi-st" id="yiyi-146">当块完成时，它将自动关闭。</span><span class="yiyi-st" id="yiyi-147">请注意，如果出现例外情况，打开的写作存档将不会最终确定；只有内部使用的文件对象将被关闭。</span><span class="yiyi-st" id="yiyi-148">有关用例，请参见<a class="reference internal" href="#tar-examples"><span>Examples</span></a>部分。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-149"><span class="versionmodified">版本3.2中的新功能：</span>添加了对上下文管理协议的支持。</span></p></div><dl class="class"><dt><span class="yiyi-st" id="yiyi-150"> <em class="property">class </em><code class="descclassname">tarfile.</code><code class="descname">TarFile</code><span class="sig-paren">(</span><em>name=None</em>, <em>mode='r'</em>, <em>fileobj=None</em>, <em>format=DEFAULT_FORMAT</em>, <em>tarinfo=TarInfo</em>, <em>dereference=False</em>, <em>ignore_zeros=False</em>, <em>encoding=ENCODING</em>, <em>errors='surrogateescape'</em>, <em>pax_headers=None</em>, <em>debug=0</em>, <em>errorlevel=0</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-151">所有以下参数是可选的，并且可以作为实例属性访问。</span></p><p><span class="yiyi-st" id="yiyi-152"><em>name</em>是归档的路径名。</span><span class="yiyi-st" id="yiyi-153">如果给出<em>fileobj</em>，则可以省略。</span><span class="yiyi-st" id="yiyi-154">在这种情况下，如果文件对象的<code class="xref py py-attr docutils literal"><span class="pre">name</span></code>属性存在，则使用该属性。</span></p><p><span class="yiyi-st" id="yiyi-155"><em>模式</em>是从现有档案读取<code class="docutils literal"><span class="pre">'r'</span></code>，<code class="docutils literal"><span class="pre">'a'</span></code>以将数据附加到现有文件，<code class="docutils literal"><span class="pre">'w'</span></code>创建新文件覆盖现有文件，或<code class="docutils literal"><span class="pre">'x'</span></code>创建新文件（如果它不存在）。</span></p><p><span class="yiyi-st" id="yiyi-156">如果给定<em>fileobj</em>，则用于读取或写入数据。</span><span class="yiyi-st" id="yiyi-157">如果可以确定，<em>fileobj</em>的模式将覆盖<em>模式</em>。</span><span class="yiyi-st" id="yiyi-158"><em>fileobj</em>将从位置0开始使用。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-159">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-160">当<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>关闭时，<em>fileobj</em>未关闭。</span></p></div><p><span class="yiyi-st" id="yiyi-161"><em>格式</em>控制归档格式。</span><span class="yiyi-st" id="yiyi-162">它必须是在模块级定义的常量<a class="reference internal" href="#tarfile.USTAR_FORMAT" title="tarfile.USTAR_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">USTAR_FORMAT</span></code></a>，<a class="reference internal" href="#tarfile.GNU_FORMAT" title="tarfile.GNU_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">GNU_FORMAT</span></code></a>或<a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">PAX_FORMAT</span></code></a>之一。</span></p><p><span class="yiyi-st" id="yiyi-163">可以使用<em>tarinfo</em>参数替换默认的<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>类。</span></p><p><span class="yiyi-st" id="yiyi-164">如果<em>解引用</em>为<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>，请向归档添加符号链接和硬链接。</span><span class="yiyi-st" id="yiyi-165">如果<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>，请将目标文件的内容添加到归档中。</span><span class="yiyi-st" id="yiyi-166">这对不支持符号链接的系统没有影响。</span></p><p><span class="yiyi-st" id="yiyi-167">如果<em>ignore_zeros</em>为<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>，则将空块视为归档的结尾。</span><span class="yiyi-st" id="yiyi-168">如果它<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>，则跳过空（和无效）块，并尝试获取尽可能多的成员。</span><span class="yiyi-st" id="yiyi-169">这仅对读取级联或损坏的存档有用。</span></p><p><span class="yiyi-st" id="yiyi-170"><em>调试</em>可以从<code class="docutils literal"><span class="pre">0</span></code>（无调试消息）到<code class="docutils literal"><span class="pre">3</span></code>（所有调试消息）设置。</span><span class="yiyi-st" id="yiyi-171">消息写入<code class="docutils literal"><span class="pre">sys.stderr</span></code>。</span></p><p><span class="yiyi-st" id="yiyi-172">如果<em>errorlevel</em>为<code class="docutils literal"><span class="pre">0</span></code>，则使用<a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal"><span class="pre">TarFile.extract()</span></code></a>时，将忽略所有错误。</span><span class="yiyi-st" id="yiyi-173">但是，当调试启用时，它们在调试输出中显示为错误消息。</span><span class="yiyi-st" id="yiyi-174">如果<code class="docutils literal"><span class="pre">1</span></code>，则所有<em>致命</em>错误都会作为<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a>异常引发。</span><span class="yiyi-st" id="yiyi-175">如果<code class="docutils literal"><span class="pre">2</span></code>，所有<em>非致命错误</em>也会作为<a class="reference internal" href="#tarfile.TarError" title="tarfile.TarError"><code class="xref py py-exc docutils literal"><span class="pre">TarError</span></code></a>异常引发。</span></p><p><span class="yiyi-st" id="yiyi-176"><em>编码</em>和<em>错误</em>参数定义要用于读取或写入归档的字符编码，以及如何处理转换错误。</span><span class="yiyi-st" id="yiyi-177">默认设置适用于大多数用户。</span><span class="yiyi-st" id="yiyi-178">有关详细信息，请参阅<a class="reference internal" href="#tar-unicode"><span>Unicode issues</span></a>一节。</span></p><p><span class="yiyi-st" id="yiyi-179"><em>pax_headers</em>参数是字符串的可选字典，如果<em>格式</em>是<a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">PAX_FORMAT</span></code></a>，则字符串将添加为pax全局标题。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-180"><span class="versionmodified">在版本3.2中已更改：</span>使用<code class="docutils literal"><span class="pre">'surrogateescape'</span></code>作为<em>错误</em>参数的默认值。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-181"><span class="versionmodified">在版本3.5中已更改：</span>添加了<code class="docutils literal"><span class="pre">'x'</span></code>（独占创建）模式。</span></p></div></dd></dl><dl class="classmethod"><dt id="tarfile.TarFile.open"><span class="yiyi-st" id="yiyi-182"> <em class="property">classmethod </em><code class="descclassname">TarFile.</code><code class="descname">open</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-183">替代构造函数。</span><span class="yiyi-st" id="yiyi-184">The <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal"><span class="pre">tarfile.open()</span></code></a> function is actually a shortcut to this classmethod.</span></p></dd></dl><dl class="method"><dt id="tarfile.TarFile.getmember"><span class="yiyi-st" id="yiyi-185"> <code class="descclassname">TarFile.</code><code class="descname">getmember</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-186">返回成员<em>名称</em>的<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-187">如果在归档中找不到<em>名称</em>，则会引发<a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></a>。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-188">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-189">如果成员在归档中出现多次，则其最后一次出现被假定为最新的版本。</span></p></div></dd></dl><dl class="method"><dt id="tarfile.TarFile.getmembers"><span class="yiyi-st" id="yiyi-190"> <code class="descclassname">TarFile.</code><code class="descname">getmembers</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-191">将归档的成员作为<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象的列表返回。</span><span class="yiyi-st" id="yiyi-192">该列表与存档中的成员具有相同的顺序。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarFile.getnames"><span class="yiyi-st" id="yiyi-193"> <code class="descclassname">TarFile.</code><code class="descname">getnames</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-194">返回成员作为他们的名字的列表。</span><span class="yiyi-st" id="yiyi-195">它与由<a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal"><span class="pre">getmembers()</span></code></a>返回的列表具有相同的顺序。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarFile.list"><span class="yiyi-st" id="yiyi-196"> <code class="descclassname">TarFile.</code><code class="descname">list</code><span class="sig-paren">(</span><em>verbose=True</em>, <em>*</em>, <em>members=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-197">将目录打印到<code class="docutils literal"><span class="pre">sys.stdout</span></code>。</span><span class="yiyi-st" id="yiyi-198">如果<em>verbose</em>是<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>，则只打印成员的名称。</span><span class="yiyi-st" id="yiyi-199">如果<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>，则产生类似于<strong class="program">ls -l 的输出。</strong></span><span class="yiyi-st" id="yiyi-200">如果给出可选的<em>成员</em>，它必须是由<a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal"><span class="pre">getmembers()</span></code></a>返回的列表的子集。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-201"><span class="versionmodified">在版本3.5中已更改：</span>添加了<em>成员</em>参数。</span></p></div></dd></dl><dl class="method"><dt id="tarfile.TarFile.next"><span class="yiyi-st" id="yiyi-202"> <code class="descclassname">TarFile.</code><code class="descname">next</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-203">当打开<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>进行读取时，将归档的下一个成员作为<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象返回。</span><span class="yiyi-st" id="yiyi-204">如果没有更多可用，返回<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarFile.extractall"><span class="yiyi-st" id="yiyi-205"> <code class="descclassname">TarFile.</code><code class="descname">extractall</code><span class="sig-paren">(</span><em>path="."</em>, <em>members=None</em>, <em>*</em>, <em>numeric_owner=False</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-206">将归档中的所有成员解压缩到当前工作目录或目录<em>路径</em>。</span><span class="yiyi-st" id="yiyi-207">如果给出了可选的<em>成员</em>，它必须是由<a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal"><span class="pre">getmembers()</span></code></a>返回的列表的子集。</span><span class="yiyi-st" id="yiyi-208">在提取所有成员后，设置所有者，修改时间和权限等目录信息。</span><span class="yiyi-st" id="yiyi-209">这是为了解决两个问题：目录的修改时间每次在其中创建文件时重置。</span><span class="yiyi-st" id="yiyi-210">并且，如果目录的权限不允许写入，则解压缩文件将失败。</span></p><p><span class="yiyi-st" id="yiyi-211">如果<em>numeric_owner</em>为<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>，tarfile中的uid和gid数字用于设置提取的文件的所有者/组。</span><span class="yiyi-st" id="yiyi-212">否则，使用来自tarfile的命名值。</span></p><div class="admonition warning"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-213">警告</span></p><p class="last"><span class="yiyi-st" id="yiyi-214">切勿在未经预先检查的情况下从不可信来源中提取存档。</span><span class="yiyi-st" id="yiyi-215">可能在<em>路径</em>之外创建文件，例如</span><span class="yiyi-st" id="yiyi-216">具有以<code class="docutils literal"><span class="pre">"/"</span></code>开头的绝对文件名的成员或具有两个点<code class="docutils literal"><span class="pre">".."</span></code>的文件名。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-217"><span class="versionmodified">在版本3.5中已更改：</span>添加了<em>numeric_only</em>参数。</span></p></div></dd></dl><dl class="method"><dt id="tarfile.TarFile.extract"><span class="yiyi-st" id="yiyi-218"> <code class="descclassname">TarFile.</code><code class="descname">extract</code><span class="sig-paren">(</span><em>member</em>, <em>path=""</em>, <em>set_attrs=True</em>, <em>*</em>, <em>numeric_owner=False</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-219">使用其全名将成员从归档提取到当前工作目录。</span><span class="yiyi-st" id="yiyi-220">其文件信息尽可能精确地提取。</span><span class="yiyi-st" id="yiyi-221"><em>成员</em>可以是文件名或<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-222">您可以使用<em>路径</em>指定其他目录。</span><span class="yiyi-st" id="yiyi-223">除非<em>set_attrs</em>为false，否则设置文件属性（owner，mtime，mode）。</span></p><p><span class="yiyi-st" id="yiyi-224">如果<em>numeric_owner</em>为<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>，tarfile中的uid和gid数字用于设置提取的文件的所有者/组。</span><span class="yiyi-st" id="yiyi-225">否则，使用来自tarfile的命名值。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-226">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-227"><a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal"><span class="pre">extract()</span></code></a>方法不处理几个提取问题。</span><span class="yiyi-st" id="yiyi-228">在大多数情况下，您应该考虑使用<a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal"><span class="pre">extractall()</span></code></a>方法。</span></p></div><div class="admonition warning"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-229">警告</span></p><p class="last"><span class="yiyi-st" id="yiyi-230">请参阅<a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal"><span class="pre">extractall()</span></code></a>的警告。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-231"><span class="versionmodified">在版本3.2中已更改：</span>添加了<em>set_attrs</em>参数。</span></p></div><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-232"><span class="versionmodified">在版本3.5中已更改：</span>添加了<em>numeric_only</em>参数。</span></p></div></dd></dl><dl class="method"><dt id="tarfile.TarFile.extractfile"><span class="yiyi-st" id="yiyi-233"> <code class="descclassname">TarFile.</code><code class="descname">extractfile</code><span class="sig-paren">(</span><em>member</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-234">从存档中提取成员作为文件对象。</span><span class="yiyi-st" id="yiyi-235"><em>成员</em>可以是文件名或<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-236">如果<em>成员</em>是常规文件或链接，则会返回<a class="reference internal" href="io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">io.BufferedReader</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-237">否则，返回<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-238"><span class="versionmodified">在版本3.3中更改：</span>返回<a class="reference internal" href="io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">io.BufferedReader</span></code></a>对象。</span></p></div></dd></dl><dl class="method"><dt id="tarfile.TarFile.add"><span class="yiyi-st" id="yiyi-239"> <code class="descclassname">TarFile.</code><code class="descname">add</code><span class="sig-paren">(</span><em>name</em>, <em>arcname=None</em>, <em>recursive=True</em>, <em>exclude=None</em>, <em>*</em>, <em>filter=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-240">将文件<em>名称</em>添加到归档中。</span><span class="yiyi-st" id="yiyi-241"><em>名称</em>可以是任何类型的文件（目录，fifo，符号链接等）。</span><span class="yiyi-st" id="yiyi-242">如果给定，<em>arcname</em>指定归档中文件的备用名称。</span><span class="yiyi-st" id="yiyi-243">默认情况下，目录以递归方式添加。</span><span class="yiyi-st" id="yiyi-244">这可以通过将<em>递归</em>设置为<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>来避免。</span><span class="yiyi-st" id="yiyi-245">如果给出<em>exclude</em>，它必须是一个函数，它需要一个文件名参数并返回一个布尔值。</span><span class="yiyi-st" id="yiyi-246">根据此值，相应的文件被排除（<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>）或添加（<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>）。</span><span class="yiyi-st" id="yiyi-247">如果指定<em>filter</em>，它必须是一个关键字参数。</span><span class="yiyi-st" id="yiyi-248">它应该是一个接受<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象参数并返回更改的<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象的函数。</span><span class="yiyi-st" id="yiyi-249">如果它返回<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal"><span class="pre">None</span></code></a>，则将从档案中排除<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-250">有关示例，请参见<a class="reference internal" href="#tar-examples"><span>Examples</span></a>。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-251"><span class="versionmodified">在版本3.2中更改：</span>添加了<em>过滤器</em>参数。</span></p></div><div class="deprecated"><p><span class="yiyi-st" id="yiyi-252"><span class="versionmodified">自版本3.2后已弃用：</span> <em>排除</em>参数已弃用，请改用<em>filter</em>参数。</span></p></div></dd></dl><dl class="method"><dt id="tarfile.TarFile.addfile"><span class="yiyi-st" id="yiyi-253"> <code class="descclassname">TarFile.</code><code class="descname">addfile</code><span class="sig-paren">(</span><em>tarinfo</em>, <em>fileobj=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-254">将<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象<em>tarinfo</em>添加到归档中。</span><span class="yiyi-st" id="yiyi-255">如果给定<em>fileobj</em>，它应该是一个<a class="reference internal" href="../glossary.html#term-binary-file"><span class="xref std std-term">binary file</span></a>，并且从它读取<code class="docutils literal"><span class="pre">tarinfo.size</span></code>字节并将其添加到归档中。</span><span class="yiyi-st" id="yiyi-256">您可以直接创建<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象，也可以使用<a class="reference internal" href="#tarfile.TarFile.gettarinfo" title="tarfile.TarFile.gettarinfo"><code class="xref py py-meth docutils literal"><span class="pre">gettarinfo()</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarFile.gettarinfo"><span class="yiyi-st" id="yiyi-257"> <code class="descclassname">TarFile.</code><code class="descname">gettarinfo</code><span class="sig-paren">(</span><em>name=None</em>, <em>arcname=None</em>, <em>fileobj=None</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-258">从现有文件的<a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal"><span class="pre">os.stat()</span></code></a>或等效结果创建<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span><span class="yiyi-st" id="yiyi-259">该文件由<em>名称</em>命名，或者用文件描述器指定为<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>fileobj</em>。</span><span class="yiyi-st" id="yiyi-260">If given, <em>arcname</em> specifies an alternative name for the file in the archive, otherwise, the name is taken from <em>fileobj</em>’s <a class="reference internal" href="io.html#io.FileIO.name" title="io.FileIO.name"><code class="xref py py-attr docutils literal"><span class="pre">name</span></code></a> attribute, or the <em>name</em> argument. </span><span class="yiyi-st" id="yiyi-261">名称应为文本字符串。</span></p><p><span class="yiyi-st" id="yiyi-262">您可以修改某些<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>的属性，然后使用<a class="reference internal" href="#tarfile.TarFile.addfile" title="tarfile.TarFile.addfile"><code class="xref py py-meth docutils literal"><span class="pre">addfile()</span></code></a>添加它。</span><span class="yiyi-st" id="yiyi-263">如果文件对象不是位于文件开头的普通文件对象，则可能需要修改<a class="reference internal" href="#tarfile.TarInfo.size" title="tarfile.TarInfo.size"><code class="xref py py-attr docutils literal"><span class="pre">size</span></code></a>等属性。</span><span class="yiyi-st" id="yiyi-264">对于诸如<a class="reference internal" href="gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>的对象就是这种情况。</span><span class="yiyi-st" id="yiyi-265">还可以修改<a class="reference internal" href="#tarfile.TarInfo.name" title="tarfile.TarInfo.name"><code class="xref py py-attr docutils literal"><span class="pre">name</span></code></a>，在这种情况下，<em>arcname</em>可以是虚拟字符串。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarFile.close"><span class="yiyi-st" id="yiyi-266"> <code class="descclassname">TarFile.</code><code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-267">关闭<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>。</span><span class="yiyi-st" id="yiyi-268">在写入模式下，两个完成零块附加到归档。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarFile.pax_headers"><span class="yiyi-st" id="yiyi-269"> <code class="descclassname">TarFile.</code><code class="descname">pax_headers</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-270">包含pax全局头的键/值对的字典。</span></p></dd></dl></div><div class="section" id="tarinfo-objects"><h2><span class="yiyi-st" id="yiyi-271">13.6.2. </span><span class="yiyi-st" id="yiyi-272">TarInfo Objects</span></h2><p><span class="yiyi-st" id="yiyi-273"><a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象表示<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>中的一个成员。</span><span class="yiyi-st" id="yiyi-274">除了存储文件的所有必需属性（如文件类型，大小，时间，权限，所有者等）</span><span class="yiyi-st" id="yiyi-275">），它提供了一些有用的方法来确定它的类型。</span><span class="yiyi-st" id="yiyi-276">它<em>不</em>包含文件的数据本身。</span></p><p><span class="yiyi-st" id="yiyi-277"><a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象由<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>的方法<code class="xref py py-meth docutils literal"><span class="pre">getmember()</span></code>，<code class="xref py py-meth docutils literal"><span class="pre">getmembers()</span></code>和<code class="xref py py-meth docutils literal"><span class="pre">gettarinfo()</span></code>。</span></p><dl class="class"><dt id="tarfile.TarInfo"><span class="yiyi-st" id="yiyi-278"> <em class="property">class </em><code class="descclassname">tarfile.</code><code class="descname">TarInfo</code><span class="sig-paren">(</span><em>name=""</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-279">创建<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span></p></dd></dl><dl class="classmethod"><dt id="tarfile.TarInfo.frombuf"><span class="yiyi-st" id="yiyi-280"> <em class="property">classmethod </em><code class="descclassname">TarInfo.</code><code class="descname">frombuf</code><span class="sig-paren">(</span><em>buf</em>, <em>encoding</em>, <em>errors</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-281">从字符串缓冲区<em>buf</em>创建并返回<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象。</span></p><p><span class="yiyi-st" id="yiyi-282">如果缓冲区无效，则引发<a class="reference internal" href="#tarfile.HeaderError" title="tarfile.HeaderError"><code class="xref py py-exc docutils literal"><span class="pre">HeaderError</span></code></a>。</span></p></dd></dl><dl class="classmethod"><dt id="tarfile.TarInfo.fromtarfile"><span class="yiyi-st" id="yiyi-283"> <em class="property">classmethod </em><code class="descclassname">TarInfo.</code><code class="descname">fromtarfile</code><span class="sig-paren">(</span><em>tarfile</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-284">从<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>对象<em>tarfile</em>中读取下一个成员，并将其作为<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象返回。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.tobuf"><span class="yiyi-st" id="yiyi-285"> <code class="descclassname">TarInfo.</code><code class="descname">tobuf</code><span class="sig-paren">(</span><em>format=DEFAULT_FORMAT</em>, <em>encoding=ENCODING</em>, <em>errors='surrogateescape'</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-286">从<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象创建字符串缓冲区。</span><span class="yiyi-st" id="yiyi-287">有关参数的信息，请参阅<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>类的构造函数。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-288"><span class="versionmodified">在版本3.2中更改：</span>使用<code class="docutils literal"><span class="pre">'surrogateescape'</span></code>作为<em>错误</em>参数的默认值。</span></p></div></dd></dl><p><span class="yiyi-st" id="yiyi-289"><code class="docutils literal"><span class="pre">TarInfo</span></code>对象具有以下公共数据属性：</span></p><dl class="attribute"><dt id="tarfile.TarInfo.name"><span class="yiyi-st" id="yiyi-290"> <code class="descclassname">TarInfo.</code><code class="descname">name</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-291">存档成员的名称。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.size"><span class="yiyi-st" id="yiyi-292"> <code class="descclassname">TarInfo.</code><code class="descname">size</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-293">大小（字节）。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.mtime"><span class="yiyi-st" id="yiyi-294"> <code class="descclassname">TarInfo.</code><code class="descname">mtime</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-295">上次修改时间。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.mode"><span class="yiyi-st" id="yiyi-296"> <code class="descclassname">TarInfo.</code><code class="descname">mode</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-297">权限位。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.type"><span class="yiyi-st" id="yiyi-298"> <code class="descclassname">TarInfo.</code><code class="descname">type</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-299">文件类型。</span><span class="yiyi-st" id="yiyi-300"><em>类型</em>通常是以下常量之一：<code class="xref py py-const docutils literal"><span class="pre">REGTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">AREGTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">LNKTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">SYMTYPE</span></code> ，<code class="xref py py-const docutils literal"><span class="pre">DIRTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">FIFOTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">CONTTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">CHRTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">BLKTYPE</span></code>，<code class="xref py py-const docutils literal"><span class="pre">GNUTYPE_SPARSE</span></code>。</span><span class="yiyi-st" id="yiyi-301">要更方便地确定<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象的类型，请使用下面的<code class="docutils literal"><span class="pre">is*()</span></code>方法。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.linkname"><span class="yiyi-st" id="yiyi-302"> <code class="descclassname">TarInfo.</code><code class="descname">linkname</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-303">目标文件名的名称，只存在于类型<code class="xref py py-const docutils literal"><span class="pre">LNKTYPE</span></code>和<code class="xref py py-const docutils literal"><span class="pre">SYMTYPE</span></code>的<a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象中。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.uid"><span class="yiyi-st" id="yiyi-304"> <code class="descclassname">TarInfo.</code><code class="descname">uid</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-305">最初存储此成员的用户的用户标识。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.gid"><span class="yiyi-st" id="yiyi-306"> <code class="descclassname">TarInfo.</code><code class="descname">gid</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-307">最初存储此成员的用户的组ID。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.uname"><span class="yiyi-st" id="yiyi-308"> <code class="descclassname">TarInfo.</code><code class="descname">uname</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-309">用户名。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.gname"><span class="yiyi-st" id="yiyi-310"> <code class="descclassname">TarInfo.</code><code class="descname">gname</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-311">组名称。</span></p></dd></dl><dl class="attribute"><dt id="tarfile.TarInfo.pax_headers"><span class="yiyi-st" id="yiyi-312"> <code class="descclassname">TarInfo.</code><code class="descname">pax_headers</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-313">包含关联的pax扩展头的键/值对的字典。</span></p></dd></dl><p><span class="yiyi-st" id="yiyi-314"><a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>对象还提供了一些方便的查询方法：</span></p><dl class="method"><dt id="tarfile.TarInfo.isfile"><span class="yiyi-st" id="yiyi-315"> <code class="descclassname">TarInfo.</code><code class="descname">isfile</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-316">如果<code class="xref py py-class docutils literal"><span class="pre">Tarinfo</span></code>对象是常规文件，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.isreg"><span class="yiyi-st" id="yiyi-317"> <code class="descclassname">TarInfo.</code><code class="descname">isreg</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-318">与<a class="reference internal" href="#tarfile.TarInfo.isfile" title="tarfile.TarInfo.isfile"><code class="xref py py-meth docutils literal"><span class="pre">isfile()</span></code></a>相同。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.isdir"><span class="yiyi-st" id="yiyi-319"> <code class="descclassname">TarInfo.</code><code class="descname">isdir</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-320">如果是目录，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.issym"><span class="yiyi-st" id="yiyi-321"> <code class="descclassname">TarInfo.</code><code class="descname">issym</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-322">如果它是符号链接，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.islnk"><span class="yiyi-st" id="yiyi-323"> <code class="descclassname">TarInfo.</code><code class="descname">islnk</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-324">如果它是硬链接，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.ischr"><span class="yiyi-st" id="yiyi-325"> <code class="descclassname">TarInfo.</code><code class="descname">ischr</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-326">如果是字符设备，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.isblk"><span class="yiyi-st" id="yiyi-327"> <code class="descclassname">TarInfo.</code><code class="descname">isblk</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-328">如果它是块设备，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.isfifo"><span class="yiyi-st" id="yiyi-329"> <code class="descclassname">TarInfo.</code><code class="descname">isfifo</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-330">如果是FIFO，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl><dl class="method"><dt id="tarfile.TarInfo.isdev"><span class="yiyi-st" id="yiyi-331"> <code class="descclassname">TarInfo.</code><code class="descname">isdev</code><span class="sig-paren">(</span><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-332">如果它是字符设备，块设备或FIFO之一，则返回<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>。</span></p></dd></dl></div><div class="section" id="command-line-interface"><h2><span class="yiyi-st" id="yiyi-333">13.6.3. </span><span class="yiyi-st" id="yiyi-334">Command Line Interface</span></h2><div class="versionadded"><p><span class="yiyi-st" id="yiyi-335"><span class="versionmodified">版本3.4中的新功能。</span></span></p></div><p><span class="yiyi-st" id="yiyi-336"><a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>模块提供了一个简单的命令行界面来与tar存档进行交互。</span></p><p><span class="yiyi-st" id="yiyi-337">如果要创建新的tar存档，请在<a class="reference internal" href="#cmdoption-c"><code class="xref std std-option docutils literal"><span class="pre">-c</span></code></a>选项后指定其名称，然后列出应包括的文件名：</span></p><div class="highlight-shell-session"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m tarfile -c monty.tar  spam.txt eggs.txt
</pre></div></div><p><span class="yiyi-st" id="yiyi-338">传递目录也是可以接受的：</span></p><div class="highlight-shell-session"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m tarfile -c monty.tar life-of-brian_1979/
</pre></div></div><p><span class="yiyi-st" id="yiyi-339">如果要将tar归档文件解压缩到当前目录中，请使用<a class="reference internal" href="#cmdoption-e"><code class="xref std std-option docutils literal"><span class="pre">-e</span></code></a>选项：</span></p><div class="highlight-shell-session"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m tarfile -e monty.tar
</pre></div></div><p><span class="yiyi-st" id="yiyi-340">您还可以通过传递目录名称将tar归档文件解压缩到其他目录中：</span></p><div class="highlight-shell-session"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m tarfile -e monty.tar  other-dir/
</pre></div></div><p><span class="yiyi-st" id="yiyi-341">有关tar归档文件中的文件列表，请使用<a class="reference internal" href="#cmdoption-l"><code class="xref std std-option docutils literal"><span class="pre">-l</span></code></a></span></p><div class="highlight-shell-session"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m tarfile -l monty.tar
</pre></div></div><div class="section" id="command-line-options"><h3><span class="yiyi-st" id="yiyi-342">13.6.3.1. </span><span class="yiyi-st" id="yiyi-343">Command line options</span></h3><dl class="cmdoption"><dt id="cmdoption-l"><span class="yiyi-st" id="yiyi-344"> <code class="descname">-l</code><code class="descclassname"> &lt;tarfile&gt;</code></span></dt><dt id="cmdoption--list"><span class="yiyi-st" id="yiyi-345"> <code class="descname">--list</code><code class="descclassname"> &lt;tarfile&gt;</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-346">在tarfile中列出文件。</span></p></dd></dl><dl class="cmdoption"><dt id="cmdoption-c"><span class="yiyi-st" id="yiyi-347"> <code class="descname">-c</code><code class="descclassname"> &lt;tarfile&gt; &lt;source1&gt; &lt;sourceN&gt;</code></span></dt><dt id="cmdoption--create"><span class="yiyi-st" id="yiyi-348"> <code class="descname">--create</code><code class="descclassname"> &lt;tarfile&gt; &lt;source1&gt; &lt;sourceN&gt;</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-349">从源文件创建tarfile。</span></p></dd></dl><dl class="cmdoption"><dt id="cmdoption-e"><span class="yiyi-st" id="yiyi-350"> <code class="descname">-e</code><code class="descclassname"> &lt;tarfile&gt; [&lt;output_dir&gt;]</code></span></dt><dt id="cmdoption--extract"><span class="yiyi-st" id="yiyi-351"> <code class="descname">--extract</code><code class="descclassname"> &lt;tarfile&gt; [&lt;output_dir&gt;]</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-352">如果未指定<em>output_dir</em>，则将tarfile提取到当前目录中。</span></p></dd></dl><dl class="cmdoption"><dt id="cmdoption-t"><span class="yiyi-st" id="yiyi-353"> <code class="descname">-t</code><code class="descclassname"> &lt;tarfile&gt;</code></span></dt><dt id="cmdoption--test"><span class="yiyi-st" id="yiyi-354"> <code class="descname">--test</code><code class="descclassname"> &lt;tarfile&gt;</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-355">测试tarfile是否有效。</span></p></dd></dl><dl class="cmdoption"><dt id="cmdoption-v"><span class="yiyi-st" id="yiyi-356"> <span id="cmdoption--verbose"></span><code class="descname">-v</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">--verbose</code><code class="descclassname"></code></span></dt><dd><p><span class="yiyi-st" id="yiyi-357">详细输出</span></p></dd></dl></div></div><div class="section" id="examples"><h2><span class="yiyi-st" id="yiyi-358">13.6.4. </span><span class="yiyi-st" id="yiyi-359">Examples</span></h2><p><span class="yiyi-st" id="yiyi-360">如何将整个tar存档提取到当前工作目录：</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"sample.tar.gz"</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre><p><span class="yiyi-st" id="yiyi-361">如何使用生成器函数而不是列表从<a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal"><span class="pre">TarFile.extractall()</span></code></a>提取tar归档文件的子集：</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">tarfile</span>

<span class="k">def</span> <span class="nf">py_files</span><span class="p">(</span><span class="n">members</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">tarinfo</span> <span class="ow">in</span> <span class="n">members</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">tarinfo</span><span class="o">.</span><span class="n">name</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s2">".py"</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">tarinfo</span>

<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"sample.tar.gz"</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">members</span><span class="o">=</span><span class="n">py_files</span><span class="p">(</span><span class="n">tar</span><span class="p">))</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre><p><span class="yiyi-st" id="yiyi-362">如何从文件名列表创建未压缩的tar存档：</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"sample.tar"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="s2">"quux"</span><span class="p">]:</span>
    <span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre><p><span class="yiyi-st" id="yiyi-363">同样的示例使用<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a>语句：</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="k">with</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"sample.tar"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span> <span class="k">as</span> <span class="n">tar</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="s2">"quux"</span><span class="p">]:</span>
        <span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
</code></pre><p><span class="yiyi-st" id="yiyi-364">如何读取gzip压缩tar存档并显示一些成员信息：</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"sample.tar.gz"</span><span class="p">,</span> <span class="s2">"r:gz"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">tarinfo</span> <span class="ow">in</span> <span class="n">tar</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">tarinfo</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s2">"is"</span><span class="p">,</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="s2">"bytes in size and is"</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isreg</span><span class="p">():</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">"a regular file."</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isdir</span><span class="p">():</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">"a directory."</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">"something else."</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre><p><span class="yiyi-st" id="yiyi-365">如何使用<a class="reference internal" href="#tarfile.TarFile.add" title="tarfile.TarFile.add"><code class="xref py py-meth docutils literal"><span class="pre">TarFile.add()</span></code></a>中的<em>过滤器</em>参数创建归档并重置用户信息：</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="n">tarinfo</span><span class="p">):</span>
    <span class="n">tarinfo</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">gid</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">tarinfo</span><span class="o">.</span><span class="n">uname</span> <span class="o">=</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">gname</span> <span class="o">=</span> <span class="s2">"root"</span>
    <span class="k">return</span> <span class="n">tarinfo</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"sample.tar.gz"</span><span class="p">,</span> <span class="s2">"w:gz"</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">reset</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre></div><div class="section" id="supported-tar-formats"><h2><span class="yiyi-st" id="yiyi-366">13.6.5. </span><span class="yiyi-st" id="yiyi-367">Supported tar formats</span></h2><p><span class="yiyi-st" id="yiyi-368">有三种tar格式可以使用<a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>模块创建：</span></p><ul><li><p class="first"><span class="yiyi-st" id="yiyi-369">POSIX.1-1988 ustar格式（<a class="reference internal" href="#tarfile.USTAR_FORMAT" title="tarfile.USTAR_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">USTAR_FORMAT</span></code></a>）。</span><span class="yiyi-st" id="yiyi-370">它支持的文件名长度最多为256个字符，链接名称最多为100个字符。</span><span class="yiyi-st" id="yiyi-371">最大文件大小为8 GiB。</span><span class="yiyi-st" id="yiyi-372">这是一个旧的和有限但广泛支持的格式。</span></p></li><li><p class="first"><span class="yiyi-st" id="yiyi-373">GNU tar格式（<a class="reference internal" href="#tarfile.GNU_FORMAT" title="tarfile.GNU_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">GNU_FORMAT</span></code></a>）。</span><span class="yiyi-st" id="yiyi-374">它支持长文件名和链接名，大于8 GiB的文件和稀疏文件。</span><span class="yiyi-st" id="yiyi-375">它是GNU / Linux系统的事实上的标准。</span><span class="yiyi-st" id="yiyi-376"><a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>完全支持长名称的GNU tar扩展，稀疏文件支持是只读的。</span></p></li><li><p class="first"><span class="yiyi-st" id="yiyi-377">POSIX.1-2001 pax格式（<a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">PAX_FORMAT</span></code></a>）。</span><span class="yiyi-st" id="yiyi-378">它是最灵活的格式，几乎没有限制。</span><span class="yiyi-st" id="yiyi-379">它支持长文件名和链接名，大文件和以便携式方式存储路径名。</span><span class="yiyi-st" id="yiyi-380">然而，并不是所有的tar实现今天能够正确处理pax档案。</span></p><p><span class="yiyi-st" id="yiyi-381"><em>pax</em>格式是对现有<em>ustar</em>格式的扩展。</span><span class="yiyi-st" id="yiyi-382">它使用额外的头信息，否则不能存储。</span><span class="yiyi-st" id="yiyi-383">有两种类型的pax头：扩展头仅影响后续文件头，全局头对整个归档有效，并影响所有以下文件。</span><span class="yiyi-st" id="yiyi-384">出于可移植性的原因，pax头中的所有数据都编码在<em>UTF-8</em>中。</span></p></li></ul><p><span class="yiyi-st" id="yiyi-385">还有一些tar格式的变体，可以读取但不创建：</span></p><ul class="simple"><li><span class="yiyi-st" id="yiyi-386">古代的V7格式。</span><span class="yiyi-st" id="yiyi-387">这是Unix第七版的第一个tar格式，只存储常规文件和目录。</span><span class="yiyi-st" id="yiyi-388">名称不能超过100个字符，没有用户/组名称信息。</span><span class="yiyi-st" id="yiyi-389">某些归档在具有非ASCII字符的字段的情况下具有错误计算的头校验和。</span></li><li><span class="yiyi-st" id="yiyi-390">SunOS tar扩展格式。</span><span class="yiyi-st" id="yiyi-391">此格式是POSIX.1-2001 pax格式的变体，但不兼容。</span></li></ul></div><div class="section" id="unicode-issues"><h2><span class="yiyi-st" id="yiyi-392">13.6.6. </span><span class="yiyi-st" id="yiyi-393">Unicode issues</span></h2><p><span class="yiyi-st" id="yiyi-394">tar格式最初是为了在磁带驱动器上进行备份，主要侧重于保留文件系统信息。</span><span class="yiyi-st" id="yiyi-395">现在tar存档通常用于文件分发和通过网络交换档案。</span><span class="yiyi-st" id="yiyi-396">原始格式（这是所有其他格式的基础）的一个问题是没有支持不同字符编码的概念。</span><span class="yiyi-st" id="yiyi-397">例如，如果<em>Latin-1</em>系统中包含非<em>ASCII</em>，则在<em>UTF-8</em>系统上创建的普通tar存档无法正确读取&gt;字符。</span><span class="yiyi-st" id="yiyi-398">文本元数据（如文件名，链接名，用户/组名称）将显示已损坏。</span><span class="yiyi-st" id="yiyi-399">不幸的是，没有办法自动检测归档的编码。</span><span class="yiyi-st" id="yiyi-400">pax格式被设计来解决这个问题。</span><span class="yiyi-st" id="yiyi-401">它使用通用字符编码<em>UTF-8</em>存储非ASCII元数据。</span></p><p><span class="yiyi-st" id="yiyi-402"><a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a>中的字符转换详细信息由<a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a>类的<em>编码</em>和<em>错误</em>关键字参数控制。</span></p><p><span class="yiyi-st" id="yiyi-403"><em>encoding</em>定义要用于存档中元数据的字符编码。</span><span class="yiyi-st" id="yiyi-404">默认值为<a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal"><span class="pre">sys.getfilesystemencoding()</span></code></a>或<code class="docutils literal"><span class="pre">'ascii'</span></code>作为后备。</span><span class="yiyi-st" id="yiyi-405">根据归档是读还是写，元数据必须解码或编码。</span><span class="yiyi-st" id="yiyi-406">如果未正确设置<em>编码</em>，则此转换可能会失败。</span></p><p><span class="yiyi-st" id="yiyi-407"><em>errors</em>参数定义如何处理无法转换的字符。</span><span class="yiyi-st" id="yiyi-408">可能的值在<a class="reference internal" href="codecs.html#error-handlers"><span>Error Handlers</span></a>部分中列出。</span><span class="yiyi-st" id="yiyi-409">默认方案是Python还用于其文件系统调用的<code class="docutils literal"><span class="pre">'surrogateescape'</span></code>，请参阅<a class="reference internal" href="os.html#os-filenames"><span>File Names, Command Line Arguments, and Environment Variables</span></a>。</span></p><p><span class="yiyi-st" id="yiyi-410">在<a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal"><span class="pre">PAX_FORMAT</span></code></a>档案的情况下，通常不需要<em>编码</em>，因为所有元数据都使用<em>UTF-8</em>存储。</span><span class="yiyi-st" id="yiyi-411"><em>encoding</em> is only used in the rare cases when binary pax headers are decoded or when strings with surrogate characters are stored.</span></p></div></div></div>